<template>
  <div class="box">
    <h2>Son1 子组件</h2>
    从vuex中获取的值: <label>{{ $store.state.totalCount }}</label>
    <br>
      <button @click="add(1)">值 + 1</button>
      <button @click="add(5)">值 + 5</button>
      <button @click="add(10)">值 + 10</button>
      <button @click="addCount(15)">值 + 15</button>
      <button @click="addCount(110)">值 + 110</button>
    </div>
</template>

<script>
import { mapMutations } from 'vuex'

export default {
  name: 'Son1Com',
  created () {
    console.log(mapMutations(['addCount']))
  },
  methods: {
    // 映射store中的mutations
    ...mapMutations(['addCount']),
    // addCount (value) {
    //   this.$store.state.totalCount += value
    // }
    add (value) {
      // ×不符合单项数据流要求
      // this.$store.state.totalCount++

      // √外部通过commit调用store中的方法(mutaions)
      this.$store.commit('addCount', value)
    }
  }
}
</script>

<style lang="css" scoped>
  .box{
    border: 3px solid #ccc;
    width: 400px;
    padding: 10px;
    margin: 20px;
  }
  h2 {
    margin-top: 10px;
  }
</style>
